/**
 * Created by Administrator on 2017/10/30 0030.
 */
var is_sumbit = false;//防止重复提交
$('#form').validate({
    submitHandler: function (form) {
        submit(form);
    },
    invalidHandler: function (form) {
    }
});

function submit(form) {
    if(is_sumbit == true)
    {
        console.log('重复提交');
        return;
    }
    is_sumbit = true;
    $.ajax({
        url: form.action,
        type: 'post',
        data: $(form).serializeArray(),
        dataType: 'json',
        success: function (res) {
            console.log(res);
            is_sumbit = false;
            if (res.status == 'SUCCESS') {
                // parent.location.reload();
                showMessage(res.message, 1);
                setTimeout(function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);

                }, 1200)

                if (res.url == '') {//url=''1.5秒刷新页面
                    setTimeout(function () {
                        //window.location.reload();
                    }, 1500);
                } else if (res.url) {//有设置URL则跳转
                    setTimeout(function () {
                       window.location.href = res.url;
                    }, 1200);
                } else {//默认不做任何跳转刷新操作
                }
            } else if (res.status == 'FAIL') {
                showMessage(res.message, 0);
            }
        },
        error: function (res) {
            is_sumbit = false;
            showMessage('error', 0);
            console.log(res, 'error');
        }
    })
}


function ajaxRequest(data, url, type) {
    if (!type) {
        type = 'GET';
    }
    $.ajax({
        type: type,
        url: url,
        data: data,
        dataType: "JSON",
        async: false,
        success: function (res) {
            if (res.status == 'SUCCESS') {
                showMessage(res.message, 1);
                if (res.url) {
                    setTimeout(function () {
                        window.location.href = res.url;
                    }, 1500)
                } else {
                    setTimeout(function () {
                        window.location.reload();
                    }, 1500)
                }
            } else if (res.status == 'FAIL') {
                showMessage(res.message, 0);
            }
        },
        error: function (res) {
            console.log(res, 'error');
        }
    });
}
